package com.icart.controller.personController;

import com.icart.context.HttpUserContext;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;

import com.icart.repositories.CustinfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.util.List;



@Controller
@RequestMapping("/person")
public class PersonOrderController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private CustinfoRepository custinfoRepository;



    /*订单首页*/
    @RequestMapping("/order")
    public String order(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        model.addAttribute("custname",userContext.getCustname());
        /*总订单展示*/
        String sql="select o.oid,o.stat,(select m.dictname from sa_db_dictitem m where m.groupid='WO_ORDERSTAT' and m.dictid=o.stat)ordertype," +
                "(select n.chnname from bi_chn n where n.chnid=o.chnid)chnname,o.cometime,o.realfee," +
                "(select count(*) from wo_infoserv v where o.custid=v.custid and o.oid=v.oid)servicesnum," +
                "(select (select m.servitemname from bi_servitem m where m.servitemid= v.servitemid)servitemname from wo_infoserv v where o.custid=v.custid and o.oid=v.oid limit 1)servitemname " +
                " from wo_info o where (o.wotype <>'sale' or o.wotype is null) and o.custid="+userContext.getCustid()+" order by o.cometime desc";
        List list =custinfoRepository.createNativeQuery(sql);
        model.addAttribute("order",list);

        /*待完工订单展示*/
        String sql2="select o.oid,o.stat,(select m.dictname from sa_db_dictitem m where m.groupid='WO_ORDERSTAT' and m.dictid=o.stat)ordertype," +
                "(select n.chnname from bi_chn n where n.chnid=o.chnid)chnname,o.cometime,o.realfee," +
                "(select count(*) from wo_infoserv v where o.custid=v.custid and o.oid=v.oid)servicesnum," +
                "(select (select m.servitemname from bi_servitem m where m.servitemid= v.servitemid)servitemname from wo_infoserv v where o.custid=v.custid and o.oid=v.oid limit 1)servitemname " +
                " from wo_info o where (o.wotype <>'sale' or o.wotype is null) and o.stat in(0,1,2,3) and o.custid="+userContext.getCustid()+" order by o.cometime desc";
        List list2 =custinfoRepository.createNativeQuery(sql2);
        model.addAttribute("workingorder",list2);

         /*待付款订单展示*/
        String sql3="select o.oid,o.stat,(select m.dictname from sa_db_dictitem m where m.groupid='WO_ORDERSTAT' and m.dictid=o.stat)ordertype," +
                "(select n.chnname from bi_chn n where n.chnid=o.chnid)chnname,o.cometime,o.realfee," +
                "(select count(*) from wo_infoserv v where o.custid=v.custid and o.oid=v.oid)servicesnum," +
                "(select (select m.servitemname from bi_servitem m where m.servitemid= v.servitemid)servitemname from wo_infoserv v where o.custid=v.custid and o.oid=v.oid limit 1)servitemname " +
                " from wo_info o where (o.wotype <>'sale' or o.wotype is null) and o.stat in(7,8) and o.custid="+userContext.getCustid()+" order by o.cometime desc";
        List list3 =custinfoRepository.createNativeQuery(sql3);
        model.addAttribute("payingorder",list3);

         /*待评价订单展示*/
        String sql4="select o.oid,o.stat,(select m.dictname from sa_db_dictitem m where m.groupid='WO_ORDERSTAT' and m.dictid=o.stat)ordertype," +
                "(select n.chnname from bi_chn n where n.chnid=o.chnid)chnname,o.cometime,o.realfee," +
                "(select count(*) from wo_infoserv v where o.custid=v.custid and o.oid=v.oid)servicesnum," +
                "(select (select m.servitemname from bi_servitem m where m.servitemid= v.servitemid)servitemname from wo_infoserv v where o.custid=v.custid and o.oid=v.oid limit 1)servitemname " +
                " from wo_info o where (o.wotype <>'sale' or o.wotype is null) and o.stat in(9) and o.custid="+userContext.getCustid()+" order by o.cometime desc";
        List list4 =custinfoRepository.createNativeQuery(sql4);
        model.addAttribute("commentingorder",list4);
        return "person/order/order";
    }


}